package com.yanqu.road.dao.impl.match;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.match.MatchWeekRankRewardState;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/3/16 21:58
 */
public class MatchWeekRankRewardDaoImpl extends TempDataObjectDao<MatchWeekRankRewardState> {
    public static String addSql = "INSERT INTO t_u_variety_week_rank_reward(userId, weekId, getReward, getRewardTime, value) values(?,?,?,?,?)";
    @Override
    protected boolean addData(MatchWeekRankRewardState data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetReward()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getGetRewardTime()));
        para.put(i, new DbParameter(Types.BIGINT, data.getValue()));
        if (getDb().executeNoneQuery(addSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE t_u_variety_week_rank_reward set getReward = ?,  getRewardTime = ?, value = ? " +
            "WHERE userId = ? AND weekId = ?";
    @Override
    protected boolean updateData(MatchWeekRankRewardState data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetReward()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getGetRewardTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getValue()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i, new DbParameter(Types.INTEGER, data.getWeekId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected MatchWeekRankRewardState parseData(ResultSet rs) throws SQLException {
        MatchWeekRankRewardState data = new MatchWeekRankRewardState();
        data.setUserId(rs.getLong("userId"));
        data.setGetReward(rs.getBoolean("getReward"));
        data.setWeekId(rs.getInt("weekId"));
        data.setGetRewardTime(rs.getLong("getRewardTime"));
        data.setValue(rs.getLong("value"));
        return data;
    }

    public static String selectSql = "SELECT * FROM t_u_variety_week_rank_reward WHERE userId = ?";
    public  Map<Integer, MatchWeekRankRewardState> getMatchWeekRankRewardStateMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, MatchWeekRankRewardState> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                MatchWeekRankRewardState data = parse(rs);
                map.put(data.getWeekId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
}
